// Module
var SPREST;
(function (SPREST) {
    // Class
    var Constants = (function () {
        function Constants() { }
        Constants.ContextInfoUrl = "_api/contextinfo";
        Constants.SiteApiUrl = "_api/site";
        Constants.WebApiUrl = "_api/web";
        return Constants;
    })();
    SPREST.Constants = Constants;    
})(SPREST || (SPREST = {}));
// Module
var SPREST;
(function (SPREST) {
    // Class
    var ListItem = (function () {
        // Constructor
        function ListItem(List, Collection, ResultData) {
            this.List = List;
            this.Collection = Collection;
            this.ResultData = ResultData;
        }
        return ListItem;
    })();
    SPREST.ListItem = ListItem;    
})(SPREST || (SPREST = {}));
///<reference path='SPRESTList.ts'/>
// Module
var SPREST;
(function (SPREST) {
    // Class
    var ListCollection = (function () {
        // Constructor
        function ListCollection(Context, Web) {
            this.Context = Context;
            this.Web = Web;
            this.lists = null;
            this.started = false;
            this.ready = false;
            this.dfd = $.Deferred();
        }
        ListCollection.prototype.start = function () {
            var myThis = this;
            if(myThis.started) {
                return myThis.dfd;
            }
            myThis.started = true;
            $.ajax(this.Web.urlLists, {
                async: true,
                headers: {
                    "Accept": "application/json;odata=verbose"
                },
                type: "GET"
            }).fail(function (req, status, error) {
                myThis.dfd.reject(myThis, req, status, error);
            }).done(function (data) {
                //alert('list collection data');
                myThis.lists = [];
                for(var looper = 0; looper < data.d.results.length; looper++) {
                    var listResult = data.d.results[looper];
                    myThis.lists.push(new SPREST.List(myThis.Context, myThis.Web, listResult));
                }
                myThis.ready = true;
                myThis.dfd.resolve(myThis);
            });
            return myThis.dfd;
        };
        ListCollection.prototype.GetListByFullUrl = function (listUrl) {
            var hostOnly = SPREST.Util.getHostOnly(listUrl);
            var serverRelativeUrl = listUrl.substring(hostOnly.length);
            return this.GetListByRelativeUrl(serverRelativeUrl);
        };
        ListCollection.prototype.GetListByRelativeUrl = function (listUrl) {
            var listsDfd = $.Deferred();
            var myThis = this;
            this.dfd.done(function (listCollection) {
                // Force start loading of all list root folders, and then join a new promise on the aggregate of them
                var dfds = [];
                for(var looper = 0; looper < myThis.lists.length; looper++) {
                    var tempDfd = myThis.lists[looper].RootFolder().start();
                    dfds.push(tempDfd);
                }
                $.when.apply(null, dfds).done(function () {
                    // All root folder queries are done
                    var lowerListUrl = listUrl.toLowerCase();
                    for(var looper = 0; looper < myThis.lists.length; looper++) {
                        var list = myThis.lists[looper];
                        var folderListUrl = list.RootFolder().serverRelativeUrl.toLowerCase();
                        if(folderListUrl == lowerListUrl) {
                            listsDfd.resolve(list);
                        }
                    }
                    listsDfd.reject(this);
                });
            });
            return listsDfd;
        };
        ListCollection.prototype.GetListByTitle = function (listTitle) {
            if(!this.ready) {
                throw new DOMException();
            }
            var listTitleLower = listTitle.toLowerCase();
            for(var looper = 0; looper < this.lists.length; looper++) {
                var curList = this.lists[looper];
                if(curList.title.toLowerCase() == listTitleLower) {
                    return curList;
                }
            }
            return null;
        };
        ListCollection.prototype.GetListByGuid = function (listGuid) {
            if(!this.ready) {
                throw new DOMException();
            }
            var listGuidLower = listGuid.toLowerCase().replace('{', '').replace('}', '');
            for(var looper = 0; looper < this.lists.length; looper++) {
                var curList = this.lists[looper];
                if(curList.id.toLowerCase() == listGuidLower) {
                    return curList;
                }
            }
            return null;
        };
        return ListCollection;
    })();
    SPREST.ListCollection = ListCollection;    
})(SPREST || (SPREST = {}));
///<reference path='SPRESTListCollection.ts'/>
// Module
var SPREST;
(function (SPREST) {
    // Class
    var Web = (function () {
        // Constructor
        function Web(Context, Site, Url) {
            if (typeof Url === "undefined") { Url = null; }
            this.Context = Context;
            this.Site = Site;
            this.Url = Url;
            this.APIUrl = null;
            this.urlFirstUniqueAncestorSecurableObject = null;
            this.urlRoleAssignments = null;
            this.urlAllProperties = null;
            this.urlAssociatedMemberGroup = null;
            this.urlAssociatedOwnerGroup = null;
            this.urlAssociatedVisitorGroup = null;
            this.urlAvailableContentTypes = null;
            this.urlAvailableFields = null;
            this.urlContentTypes = null;
            this.urlCurrentUser = null;
            this.urlEventReceivers = null;
            this.urlFeatures = null;
            this.urlFields = null;
            this.urlFolders = null;
            this.urlLists = null;
            this.urlListTemplates = null;
            this.urlNavigation = null;
            this.urlParentWeb = null;
            this.urlPushNotificationSubscribers = null;
            this.urlRecycleBin = null;
            this.urlRegionalSettings = null;
            this.urlRoleDefinitions = null;
            this.urlRootFolder = null;
            this.urlSiteGroups = null;
            this.urlSiteUserInfoList = null;
            this.urlSiteUsers = null;
            this.urlThemeInfo = null;
            this.urlUserCustomActions = null;
            this.urlWebs = null;
            this.urlWebInfos = null;
            this.urlWorkflowAssociations = null;
            this.urlWorkflowTemplates = null;
            this.allowRssFeeds = null;
            this.appInstanceId = null;
            this.configuration = null;
            this.created = null;
            this.customMasterUrl = null;
            this.description = null;
            this.documentLibraryCalloutOfficeWebAppPreviewersDisabled = null;
            this.enableMinimalDownload = null;
            this.id = null;
            this.language = null;
            this.lastItemModifiedDate = null;
            this.masterUrl = null;
            this.quickLaunchEnabled = null;
            this.recycleBinEnabled = null;
            this.serverRelativeUrl = null;
            this.syndicationEnabled = null;
            this.title = null;
            this.treeViewEnabled = null;
            this.uIVersion = null;
            this.uIVersionConfigurationEnabled = null;
            this.reportedUrl = null;
            this.webTemplate = null;
            this.allLists = null;
            this.started = false;
            this.ready = false;
            this.dfd = $.Deferred();
        }
        Web.prototype.GetLists = function () {
            if(this.allLists != null) {
                return this.allLists;
            }
            var lc = new SPREST.ListCollection(this.Context, this);
            this.allLists = lc;
            var dfd = lc.start();
            return lc;
        };
        Web.prototype.start = function () {
            var myThis = this;
            if(myThis.started) {
                return (myThis.dfd);
            }
            myThis.started = true;
            myThis.APIUrl = SPREST.Util.concatenateUrl(this.Context.contextWebUrl, SPREST.Constants.WebApiUrl);
            $.ajax(myThis.APIUrl, {
                async: true,
                headers: {
                    "Accept": "application/json;odata=verbose"
                },
                type: "GET"
            }).fail(function (req, status, error) {
                myThis.dfd.reject(myThis, req, status, error);
            }).done(function (data) {
                myThis.urlFirstUniqueAncestorSecurableObject = data.d.FirstUniqueAncestorSecurableObject.__deferred.uri;
                myThis.urlRoleAssignments = data.d.RoleAssignments.__deferred.uri;
                myThis.urlAllProperties = data.d.AllProperties.__deferred.uri;
                myThis.urlAssociatedMemberGroup = data.d.AssociatedMemberGroup.__deferred.uri;
                myThis.urlAssociatedOwnerGroup = data.d.AssociatedOwnerGroup.__deferred.uri;
                myThis.urlAssociatedVisitorGroup = data.d.AssociatedVisitorGroup.__deferred.uri;
                myThis.urlAvailableContentTypes = data.d.AvailableContentTypes.__deferred.uri;
                myThis.urlAvailableFields = data.d.AvailableFields.__deferred.uri;
                myThis.urlContentTypes = data.d.ContentTypes.__deferred.uri;
                myThis.urlCurrentUser = data.d.CurrentUser.__deferred.uri;
                myThis.urlEventReceivers = data.d.EventReceivers.__deferred.uri;
                myThis.urlFeatures = data.d.Features.__deferred.uri;
                myThis.urlFields = data.d.Fields.__deferred.uri;
                myThis.urlFolders = data.d.Folders.__deferred.uri;
                myThis.urlLists = data.d.Lists.__deferred.uri;
                myThis.urlListTemplates = data.d.ListTemplates.__deferred.uri;
                myThis.urlNavigation = data.d.Navigation.__deferred.uri;
                myThis.urlParentWeb = data.d.ParentWeb.__deferred.uri;
                myThis.urlPushNotificationSubscribers = data.d.PushNotificationSubscribers.__deferred.uri;
                myThis.urlRecycleBin = data.d.RecycleBin.__deferred.uri;
                myThis.urlRegionalSettings = data.d.RegionalSettings.__deferred.uri;
                myThis.urlRoleDefinitions = data.d.RoleDefinitions.__deferred.uri;
                myThis.urlRootFolder = data.d.RootFolder.__deferred.uri;
                myThis.urlSiteGroups = data.d.SiteGroups.__deferred.uri;
                myThis.urlSiteUserInfoList = data.d.SiteUserInfoList.__deferred.uri;
                myThis.urlSiteUsers = data.d.SiteUsers.__deferred.uri;
                myThis.urlThemeInfo = data.d.ThemeInfo.__deferred.uri;
                myThis.urlUserCustomActions = data.d.UserCustomActions.__deferred.uri;
                myThis.urlWebs = data.d.Webs.__deferred.uri;
                myThis.urlWebInfos = data.d.WebInfos.__deferred.uri;
                myThis.urlWorkflowAssociations = data.d.WorkflowAssociations.__deferred.uri;
                myThis.urlWorkflowTemplates = data.d.WorkflowTemplates.__deferred.uri;
                myThis.allowRssFeeds = data.d.AllowRssFeeds;
                myThis.appInstanceId = data.d.AppInstanceId;
                myThis.configuration = data.d.Configuration;
                myThis.created = data.d.Created;
                myThis.customMasterUrl = data.d.CustomMasterUrl;
                myThis.description = data.d.Description;
                myThis.documentLibraryCalloutOfficeWebAppPreviewersDisabled = data.d.DocumentLibraryCalloutOfficeWebAppPreviewersDisabled;
                myThis.enableMinimalDownload = data.d.EnableMinimalDownload;
                myThis.id = data.d.Id;
                myThis.language = data.d.Language;
                myThis.lastItemModifiedDate = data.d.LastItemModifiedDate;
                myThis.masterUrl = data.d.MasterUrl;
                myThis.quickLaunchEnabled = data.d.QuickLaunchEnabled;
                myThis.recycleBinEnabled = data.d.RecycleBinEnabled;
                myThis.serverRelativeUrl = data.d.ServerRelativeUrl;
                myThis.syndicationEnabled = data.d.SyndicationEnabled;
                myThis.title = data.d.Title;
                myThis.treeViewEnabled = data.d.TreeViewEnabled;
                myThis.uIVersion = data.d.UIVersion;
                myThis.uIVersionConfigurationEnabled = data.d.UIVersionConfigurationEnabled;
                myThis.reportedUrl = data.d.Url;
                myThis.webTemplate = data.d.WebTemplate;
                myThis.ready = true;
                myThis.dfd.resolve(myThis);
            });
            return myThis.dfd;
        };
        Web.prototype.GetFolderByServerRelativeUrl = function (url) {
            var APIUrl = SPREST.Util.concatenateUrl(this.APIUrl, "GetFolderByServerRelativeUrl('" + url + "')");
            var folder = new SPREST.Folder(this.Context, this, APIUrl);
            folder.start();
            return folder;
        };
        return Web;
    })();
    SPREST.Web = Web;    
})(SPREST || (SPREST = {}));
///<reference path='SPRESTWeb.ts'/>
// Module
var SPREST;
(function (SPREST) {
    // Class
    var WebCollection = (function () {
        // Constructor
        function WebCollection(Context, Site) {
            this.Context = Context;
            this.Site = Site;
        }
        WebCollection.prototype.start = function () {
            // TODO: Start
            return null;
        };
        WebCollection.prototype.GetAllWebs = function () {
            // TODO: Get all Webs
            return null;
        };
        WebCollection.prototype.GetWebByUrl = function () {
            // TODO: Get Web by Url
            return null;
        };
        WebCollection.prototype.GetWebByTitle = function () {
            // TODO: Get Web by Title
            return null;
        };
        return WebCollection;
    })();
    SPREST.WebCollection = WebCollection;    
})(SPREST || (SPREST = {}));
// Module
var SPREST;
(function (SPREST) {
    // Class
    var Util = (function () {
        function Util() { }
        Util.concatenateUrl = function concatenateUrl(pt1, pt2) {
            if(pt1 == null || pt2 == null) {
                return null;
            }
            var pt1End = pt1[pt1.length - 1];
            var pt2Start = pt2[0];
            if(pt1End == '/' && pt2Start == '/') {
                return pt1 + pt2.substring(1);
            } else if(pt1End != '/' && pt2Start != '/') {
                return pt1 + '/' + pt2;
            } else {
                return pt1 + pt2;
            }
        };
        Util.getHostOnly = function getHostOnly(fullUrl) {
            var slashPos = 0;
            slashPos = fullUrl.indexOf('/');
            if(slashPos > 0) {
                // 2nd from protocol
                slashPos = fullUrl.indexOf('/', slashPos + 1);
                if(slashPos > 0) {
                    // 3rd (after hostname)
                    slashPos = fullUrl.indexOf('/', slashPos + 1);
                    if(slashPos == 0) {
                        // No trailing slash
                        slashPos = fullUrl.length;
                    }
                }
            }
            return (fullUrl.substring(0, slashPos));
        };
        return Util;
    })();
    SPREST.Util = Util;    
})(SPREST || (SPREST = {}));
///<reference path='SPRESTContext.ts'/>
///<reference path='SPRESTWebCollection.ts'/>
///<reference path='SPRESTUtil.ts'/>
// Module
var SPREST;
(function (SPREST) {
    // Class
    var Site = (function () {
        // Constructor
        function Site(Context) {
            this.Context = Context;
            this.contextInfoSiteUrl = null;
            this.started = false;
            this.ready = false;
            this.dfd = $.Deferred();
            this.urlEventReceiver = null;
            this.urlFeatures = null;
            this.urlOwner = null;
            this.urlRecycleBin = null;
            this.urlRootWeb = null;
            this.urlUserCustomActions = null;
            this.allowDesigner = null;
            this.allowMasterPageEditing = null;
            this.allowRevertFromTemplate = null;
            this.allowSelfServiceUpgrade = null;
            this.allowSelfServiceUpgradeEvaluation = null;
            this.compatibilityLevel = null;
            this.id = null;
            this.maxItemsPerThrottledOperation = null;
            this.primaryUri = null;
            this.readOnly = null;
            this.serverRelativeUrl = null;
            this.shareByLinkEnabled = null;
            this.showUrlStructure = null;
            this.uIVersionConfigurationEnabled = null;
            this.upgradeReminderDate = null;
            this.upgrading = null;
            this.url = null;
        }
        Site.prototype.GetWebs = function () {
            var webs = new SPREST.WebCollection(this.Context, this);
            var dfd = webs.start();
            return webs;
        };
        Site.prototype.GetWeb = function () {
            var web = new SPREST.Web(this.Context, this, this.Context.contextWebUrl);
            var dfd = web.start();
            return web;
        };
        Site.prototype.start = function () {
            var myThis = this;
            if(myThis.started) {
                return myThis.dfd;
            }
            myThis.started = true;
            var siteInfoUrl = SPREST.Util.concatenateUrl(this.Context.contextSiteUrl, SPREST.Constants.SiteApiUrl);
            $.ajax(siteInfoUrl, {
                async: true,
                headers: {
                    "Accept": "application/json;odata=verbose"
                },
                type: "GET"
            }).fail(function (req, status, error) {
                myThis.dfd.reject(myThis, req, status, error);
            }).done(function (data) {
                myThis.urlEventReceiver = data.d.EventReceivers.__deferred.uri;
                myThis.urlFeatures = data.d.Features.__deferred.uri;
                myThis.urlOwner = data.d.Owner.__deferred.uri;
                myThis.urlRecycleBin = data.d.RecycleBin.__deferred.uri;
                myThis.urlRootWeb = data.d.RootWeb.__deferred.uri;
                myThis.urlUserCustomActions = data.d.UserCustomActions.__deferred.uri;
                myThis.allowDesigner = data.d.AllowDesigner;
                myThis.allowMasterPageEditing = data.d.AllowMasterPageEditing;
                myThis.allowRevertFromTemplate = data.d.AllowRevertFromTemplate;
                myThis.allowSelfServiceUpgrade = data.d.AllowSelfServiceUpgrade;
                myThis.allowSelfServiceUpgradeEvaluation = data.d.AllowSelfServiceUpgradeEvaluation;
                myThis.compatibilityLevel = data.d.CompatibilityLevel;
                myThis.id = data.d.Id;
                myThis.maxItemsPerThrottledOperation = data.d.MaxItemsPerThrottledOperation;
                myThis.primaryUri = data.d.PrimaryUri;
                myThis.readOnly = data.d.ReadOnly;
                myThis.serverRelativeUrl = data.d.ServerRelativeUrl;
                myThis.shareByLinkEnabled = data.d.ShareByLinkEnabled;
                myThis.showUrlStructure = data.d.ShowUrlStructure;
                myThis.uIVersionConfigurationEnabled = data.d.UIVersionConfigurationEnabled;
                myThis.upgradeReminderDate = data.d.UpgradeReminderDate;
                myThis.upgrading = data.d.Upgrading;
                myThis.url = data.d.Url;
                myThis.ready = true;
                myThis.dfd.resolve(myThis);
            });
            return myThis.dfd;
        };
        return Site;
    })();
    SPREST.Site = Site;    
})(SPREST || (SPREST = {}));
///<reference path='jquery.d.ts' />
///<reference path='SPRESTSite.ts'/>
///<reference path='SPRESTUtil.ts'/>
///<reference path='SPRESTConstants.ts'/>
// Module
var SPREST;
(function (SPREST) {
    // Class
    var Context = (function () {
        // Constructor
        function Context(ContextUrl) {
            this.ContextUrl = ContextUrl;
            this.contextInfoUrl = null;
            this.serverVersion = 0;
            this.contextSiteUrl = null;
            this.contextWebUrl = null;
            this.RequestDigest = null;
            this.ready = false;
            this.started = false;
            this.dfd = $.Deferred();
            this.siteProp = null;
            this.webProp = null;
            // Find SharePoint
            this.contextInfoUrl = SPREST.Util.concatenateUrl(ContextUrl, SPREST.Constants.ContextInfoUrl);
            this.start();
        }
        Context.contexts = null;
        Context.GetContext = function GetContext(contextUrl) {
            if(Context.contexts == null) {
                Context.contexts = [];
                var context = new SPREST.Context(contextUrl);
                Context.contexts.push(context);
                return context;
            }
            var contextUrlLower = contextUrl.toLowerCase();
            for(var looper = 0; looper < Context.contexts.length; looper++) {
                var curContext = Context.contexts[looper];
                if(curContext.ContextUrl.toLowerCase() == contextUrlLower) {
                    return curContext;
                }
            }
            var context = new SPREST.Context(contextUrl);
            Context.contexts.push(context);
            return context;
        };
        Context.prototype.getSite = function () {
            if(this.siteProp != null) {
                return this.siteProp;
            }
            this.siteProp = new SPREST.Site(this);
            var dfd = this.siteProp.start();
            return this.siteProp;
        };
        Context.prototype.getWeb = function () {
            // Note: that here we won't call site.start
            if(this.siteProp == null) {
                this.siteProp = new SPREST.Site(this);
            }
            if(this.webProp != null) {
                return this.webProp;
            }
            this.webProp = new SPREST.Web(this, this.siteProp, this.contextWebUrl);
            var dfd = this.webProp.start();
            return this.webProp;
        };
        Context.prototype.start = function () {
            var myThis = this;
            if(myThis.started) {
                return this.dfd;
            }
            myThis.started = true;
            $.ajaxSetup({
                cache: false,
                crossDomain: true
            });
            $.support.cors = true;
            $.ajax(this.contextInfoUrl, {
                async: true,
                headers: {
                    "Accept": "application/json;odata=verbose"
                },
                type: "POST"
            }).fail(function (req, status, error) {
                myThis.dfd.reject(myThis, req, status, error);
            }).done(function (data) {
                myThis.RequestDigest = data.d.GetContextWebInformation.FormDigestValue;
                myThis.serverVersion = data.d.GetContextWebInformation.LibraryVersion;
                myThis.contextSiteUrl = data.d.GetContextWebInformation.SiteFullUrl;
                myThis.contextWebUrl = data.d.GetContextWebInformation.WebFullUrl;
                // TODO: Supported Schema Versions
                myThis.ready = true;
                myThis.dfd.resolve(myThis);
            });
            return myThis.dfd;
        };
        return Context;
    })();
    SPREST.Context = Context;    
})(SPREST || (SPREST = {}));
///<reference path='SPRESTList.ts'/>
///<reference path='SPRESTContext.ts'/>
///<reference path='SPRESTWeb.ts'/>
// Module
var SPREST;
(function (SPREST) {
    // Class
    var File = (function () {
        function File(Context, Web, Folder, files, ResultData) {
            this.Context = Context;
            this.Web = Web;
            this.Folder = Folder;
            this.files = files;
            this.ResultData = ResultData;
            this.checkInComment = null;
            this.checkOutType = null;
            this.contentTag = null;
            this.customizedPageStatus = null;
            this.eTag = null;
            this.exists = null;
            this.length = null;
            this.majorVersion = null;
            this.minorVersion = null;
            this.name = null;
            this.serverRelativeUrl = null;
            this.timeCreated = null;
            this.timeLastModified = null;
            this.title = null;
            this.uIVersion = null;
            this.uIVersionLabel = null;
            this.checkInComment = ResultData.CheckInComment;
            this.checkOutType = ResultData.CheckOutType;
            this.contentTag = ResultData.ContentTag;
            this.customizedPageStatus = ResultData.CustomizedPageStatus;
            this.eTag = ResultData.ETag;
            this.exists = ResultData.Exists;
            this.length = ResultData.Length;
            this.majorVersion = ResultData.MajorVersion;
            this.minorVersion = ResultData.MinorVersion;
            this.name = ResultData.Name;
            this.serverRelativeUrl = ResultData.ServerRelativeUrl;
            this.timeCreated = ResultData.TimeCreated;
            this.timeLastModified = ResultData.TimeLastModified;
            this.title = ResultData.Title;
            this.uIVersion = ResultData.UIVersion;
            this.uIVersionLabel = ResultData.UIVersionLabel;
        }
        return File;
    })();
    SPREST.File = File;    
})(SPREST || (SPREST = {}));
///<reference path='SPRESTList.ts'/>
///<reference path='SPRESTContext.ts'/>
///<reference path='SPRESTWeb.ts'/>
///<reference path='SPRESTFile.ts'/>
// Module
var SPREST;
(function (SPREST) {
    // Class
    var FileCollection = (function () {
        function FileCollection(Context, Web, Folder, APIUrl) {
            this.Context = Context;
            this.Web = Web;
            this.Folder = Folder;
            this.APIUrl = APIUrl;
            this.started = false;
            this.ready = false;
            this.dfd = $.Deferred();
            this.files = null;
        }
        FileCollection.prototype.start = function () {
            var myThis = this;
            if(myThis.started) {
                return myThis.dfd;
            }
            myThis.started = true;
            $.ajax(myThis.APIUrl, {
                async: true,
                headers: {
                    "Accept": "application/json;odata=verbose"
                },
                type: "GET"
            }).fail(function (req, status, error) {
                myThis.dfd.reject(myThis, req, status, error);
            }).done(function (data) {
                myThis.files = [];
                for(var looper = 0; looper < data.d.results.length; looper++) {
                    myThis.files.push(new SPREST.File(myThis.Context, myThis.Web, myThis.Folder, myThis, data.d.results[looper]));
                }
                myThis.ready = true;
                myThis.dfd.resolve(myThis);
            });
            return myThis.dfd;
        };
        return FileCollection;
    })();
    SPREST.FileCollection = FileCollection;    
})(SPREST || (SPREST = {}));
///<reference path='SPRESTList.ts'/>
///<reference path='SPRESTContext.ts'/>
///<reference path='SPRESTWeb.ts'/>
///<reference path='SPRESTFileCollection.ts'/>
// Module
var SPREST;
(function (SPREST) {
    // Class
    var Folder = (function () {
        function Folder(Context, Web, apiUrl) {
            this.Context = Context;
            this.Web = Web;
            this.apiUrl = apiUrl;
            this.urlFiles = null;
            this.urlListItemAllFields = null;
            this.urlParentFolder = null;
            this.urlProperties = null;
            this.urlFolders = null;
            this.itemCount = null;
            this.name = null;
            this.serverRelativeUrl = null;
            this.welcomePage = null;
            this.started = false;
            this.ready = false;
            this.dfd = $.Deferred();
        }
        Folder.prototype.start = function () {
            var myThis = this;
            if(myThis.started) {
                return myThis.dfd;
            }
            myThis.started = true;
            $.ajax(myThis.apiUrl, {
                async: true,
                headers: {
                    "Accept": "application/json;odata=verbose"
                },
                type: "GET"
            }).fail(function (req, status, error) {
                myThis.dfd.reject(myThis, req, status, error);
            }).done(function (data) {
                myThis.urlFiles = data.d.Files.__deferred.uri;
                myThis.urlListItemAllFields = data.d.ListItemAllFields.__deferred.uri;
                myThis.urlParentFolder = data.d.ParentFolder.__deferred.uri;
                myThis.urlProperties = data.d.Properties.__deferred.uri;
                myThis.urlFolders = data.d.Folders.__deferred.uri;
                myThis.itemCount = data.d.ItemCount;
                myThis.name = data.d.Name;
                myThis.serverRelativeUrl = data.d.ServerRelativeUrl;
                myThis.welcomePage = data.d.WelcomePage;
                myThis.ready = true;
                myThis.dfd.resolve(myThis);
            });
            return myThis.dfd;
        };
        Folder.prototype.getFiles = function () {
            var files = new SPREST.FileCollection(this.Context, this.Web, this, this.urlFiles);
            files.start();
            return files;
        };
        return Folder;
    })();
    SPREST.Folder = Folder;    
})(SPREST || (SPREST = {}));
///<reference path='SPRESTList.ts'/>
///<reference path='SPRESTContext.ts'/>
///<reference path='SPRESTWeb.ts'/>
// Module
var SPREST;
(function (SPREST) {
    // Class
    var ListItemCollection = (function () {
        function ListItemCollection(Context, List, QueryUrl) {
            this.Context = Context;
            this.List = List;
            this.QueryUrl = QueryUrl;
            this.started = false;
            this.ready = false;
            this.dfd = $.Deferred();
        }
        ListItemCollection.prototype.start = function () {
            var myThis = this;
            if(myThis.started) {
                return myThis.dfd;
            }
            myThis.started = true;
            $.ajax(myThis.QueryUrl, {
                async: true,
                headers: {
                    "Accept": "application/json;odata=verbose"
                },
                type: "GET"
            }).fail(function (req, status, error) {
                myThis.dfd.reject(myThis, req, status, error);
            }).done(function (data) {
                myThis.items = [];
                for(var looper = 0; looper < data.d.results.length; looper++) {
                    var resultData = data.d.results[looper];
                    var li = new SPREST.ListItem(myThis.List, myThis, resultData);
                    myThis.items.push(li);
                }
                myThis.ready = true;
                myThis.dfd.resolve(myThis);
            });
            return myThis.dfd;
        };
        return ListItemCollection;
    })();
    SPREST.ListItemCollection = ListItemCollection;    
})(SPREST || (SPREST = {}));
///<reference path='SPRESTList.ts'/>
///<reference path='SPRESTContext.ts'/>
///<reference path='SPRESTWeb.ts'/>
///<reference path='SPRESTContentType.ts'/>
// Module
var SPREST;
(function (SPREST) {
    // Class
    var ContentTypeCollection = (function () {
        function ContentTypeCollection(Context, Web, urlApi) {
            this.Context = Context;
            this.Web = Web;
            this.urlApi = urlApi;
            this.started = false;
            this.ready = false;
            this.dfd = $.Deferred();
        }
        ContentTypeCollection.prototype.start = function () {
            var myThis = this;
            if(myThis.started) {
                return myThis.dfd;
            }
            myThis.started = true;
            $.ajax(myThis.urlApi, {
                async: true,
                headers: {
                    "Accept": "application/json;odata=verbose"
                },
                type: "GET"
            }).fail(function (req, status, error) {
                myThis.dfd.reject(myThis, req, status, error);
            }).done(function (data) {
                alert('content type collection data');
                myThis.items = [];
                for(var looper = 0; looper < data.d.results.length; looper++) {
                    var resultData = data.d.results[looper];
                    var li = new SPREST.ContentType(myThis.Context, myThis.Web, resultData);
                    myThis.items.push(li);
                }
                myThis.ready = true;
                myThis.dfd.resolve(myThis);
            });
            return myThis.dfd;
        };
        return ContentTypeCollection;
    })();
    SPREST.ContentTypeCollection = ContentTypeCollection;    
})(SPREST || (SPREST = {}));
///<reference path='SPRESTListItem.ts'/>
///<reference path='SPRESTFolder.ts' />
///<reference path='SPRESTListItemCollection.ts' />
///<reference path='SPRESTContentTypeCollection.ts' />
// Module
var SPREST;
(function (SPREST) {
    // eq, neq, gt, lt, gte, lte
    // Class
    var List = (function () {
        // Constructor
        function List(Context, Web, listData) {
            this.Context = Context;
            this.Web = Web;
            this.urlFirstUniqueAncestorSecurableObject = null;
            this.urlRoleAssignments = null;
            this.urlContentTypes = null;
            this.urlDefaultView = null;
            this.urlEventReceivers = null;
            this.urlFields = null;
            this.urlForms = null;
            this.urlInformationRightsManagementSettings = null;
            this.urlItems = null;
            this.urlParentWeb = null;
            this.urlRootFolder = null;
            this.urlUserCustomActions = null;
            this.urlViews = null;
            this.urlWorkflowAssociations = null;
            this.allowContentTypes = null;
            this.baseTemplate = null;
            this.baseType = null;
            this.contentTypesEnabled = null;
            this.created = null;
            this.defaultContentApprovalWorkflowId = null;
            this.description = null;
            this.direction = null;
            this.documentTemplateUrl = null;
            this.draftVersionVisibility = null;
            this.enableAttachments = false;
            this.enableFolderCreation = null;
            this.enableMinorVersions = null;
            this.enableModeration = null;
            this.enableVersioning = null;
            this.entityTypeName = null;
            this.forceCheckout = null;
            this.hasExternalDataSource = null;
            this.hidden = null;
            this.id = null;
            this.imageUrl = null;
            this.irmEnabled = null;
            this.irmExpire = null;
            this.irmReject = null;
            this.isApplicationList = null;
            this.isCatalog = null;
            this.isPrivate = null;
            this.itemCount = null;
            this.lastItemDeletedDate = null;
            this.lastItemModifiedDate = null;
            this.listItemEntityTypeFullName = null;
            this.multipleDataList = null;
            this.noCrawl = null;
            this.parentWebUrl = null;
            this.serverTemplateCanCreateFolders = null;
            this.templateFeatureId = null;
            this.title = null;
            this.contentTypeProp = null;
            this.urlFirstUniqueAncestorSecurableObject = listData.FirstUniqueAncestorSecurableObject.__deferred.uri;
            this.urlRoleAssignments = listData.RoleAssignments.__deferred.uri;
            this.urlContentTypes = listData.ContentTypes.__deferred.uri;
            this.urlDefaultView = listData.ContentTypes.__deferred.uri;
            this.urlEventReceivers = listData.EventReceivers.__deferred.uri;
            this.urlFields = listData.Fields.__deferred.uri;
            this.urlForms = listData.Forms.__deferred.uri;
            this.urlInformationRightsManagementSettings = listData.InformationRightsManagementSettings.__deferred.uri;
            this.urlItems = listData.Items.__deferred.uri;
            this.urlParentWeb = listData.ParentWeb.__deferred.uri;
            this.urlRootFolder = listData.RootFolder.__deferred.uri;
            this.urlUserCustomActions = listData.UserCustomActions.__deferred.uri;
            this.urlViews = listData.Views.__deferred.uri;
            this.urlWorkflowAssociations = listData.WorkflowAssociations.__deferred.uri;
            this.allowContentTypes = listData.AllowContentTypes;
            this.baseTemplate = listData.BaseTemplate;
            this.baseType = listData.BaseType;
            this.contentTypesEnabled = listData.ContentTypesEnabled;
            this.created = listData.Created;
            this.defaultContentApprovalWorkflowId = listData.DefaultContentApprovalWorkflowId;
            this.description = listData.Description;
            this.direction = listData.Direction;
            this.documentTemplateUrl = listData.DocumentTemplateUrl;
            this.draftVersionVisibility = listData.DraftVersionVisibility;
            this.enableAttachments = listData.EnableAttachments;
            this.enableFolderCreation = listData.EnableFolderCreation;
            this.enableMinorVersions = listData.EnabledMinorVersions;
            this.enableModeration = listData.EnableModeration;
            this.enableVersioning = listData.EnableVersioning;
            this.entityTypeName = listData.EntityTypeName;
            this.forceCheckout = listData.ForceCheckout;
            this.hasExternalDataSource = listData.HasExternalDataSource;
            this.hidden = listData.Hidden;
            this.id = listData.Id;
            this.imageUrl = listData.ImageUrl;
            this.irmEnabled = listData.IrmEnabled;
            this.irmExpire = listData.IrmExpire;
            this.irmReject = listData.IrmReject;
            this.isApplicationList = listData.IsApplicationList;
            this.isCatalog = listData.IsCatalog;
            this.isPrivate = listData.IsPrivate;
            this.itemCount = listData.ItemCount;
            this.lastItemDeletedDate = listData.LastItemDeletedDate;
            this.lastItemModifiedDate = listData.LastItemModifiedDate;
            this.listItemEntityTypeFullName = listData.ListItemEntityTypeFullName;
            this.multipleDataList = listData.MultipleDataList;
            this.noCrawl = listData.NoCrawl;
            this.parentWebUrl = listData.ParentWebUrl;
            this.serverTemplateCanCreateFolders = listData.ServerTemplateCanCreateFolders;
            this.templateFeatureId = listData.TemplateFeatureId;
            this.title = listData.Title;
        }
        List.prototype.RootFolder = function () {
            if(this.rootFolderProperty != null) {
                return this.rootFolderProperty;
            }
            var folder = new SPREST.Folder(this.Context, this.Web, this.urlRootFolder);
            this.rootFolderProperty = folder;
            var dfd = folder.start();
            return folder;
        };
        List.prototype.GetAllItems = function () {
            var items = new SPREST.ListItemCollection(this.Context, this, this.urlItems);
            items.start();
            return items;
        };
        List.prototype.GetItemsByQuery = function (queryParameters) {
            var listUrl = this.urlItems + '?' + this.GetFullFilterString(queryParameters);
            var items = new SPREST.ListItemCollection(this.Context, this, listUrl);
            items.start();
            return items;
        };
        List.prototype.GetFullFilterString = function (criteria) {
            var result = '$filter=';
            for(var looper = 0; looper < criteria.length; looper++) {
                result += this.GetFilterString(criteria[looper]);
                if(looper + 1 < criteria.length) {
                    result += ' and ';
                }
            }
            return result;
        };
        List.prototype.GetFilterString = function (criteria) {
            var result = criteria.field + " " + criteria.operator.toLowerCase() + " '" + encodeURIComponent(criteria.value) + "'";
            return result;
        };
        List.prototype.AddItem = function (item) {
            var dfd = $.Deferred();
            var myThis = this;
            var myItem = {
                "__metadata": {
                    'type': myThis.listItemEntityTypeFullName
                }
            };
            for(var prop in item) {
                myItem[prop] = item[prop];
            }
            var ajaxSettings = {
                async: true,
                contentType: "application/json;odata=verbose",
                headers: {
                    "Accept": "application/json;odata=verbose",
                    "X-RequestDigest": myThis.Context.RequestDigest
                },
                type: "POST",
                data: JSON.stringify(myItem)
            };
            $.ajax(myThis.urlItems, ajaxSettings).fail(function (req, status, error) {
                dfd.reject(myThis, req, status, error);
            }).done(function (data) {
                var newItem = new SPREST.ListItem(myThis, null, data.d);
                dfd.resolve(newItem);
            });
            return dfd;
        };
        List.prototype.GetContentTypes = function () {
            if(this.contentTypeProp != null) {
                return this.contentTypeProp;
            }
            var ctColl = new SPREST.ContentTypeCollection(this.Context, this.Web, this.urlContentTypes);
            var dfd = ctColl.start();
            return ctColl;
        };
        return List;
    })();
    SPREST.List = List;    
})(SPREST || (SPREST = {}));
///<reference path='SPRESTList.ts'/>
///<reference path='SPRESTContext.ts'/>
///<reference path='SPRESTWeb.ts'/>
// Module
var SPREST;
(function (SPREST) {
    // Class
    var ContentType = (function () {
        function ContentType(Context, Web, resultData) {
            this.Context = Context;
            this.Web = Web;
            this.urlFieldLinks = null;
            this.urlFields = null;
            this.urlParent = null;
            this.urlWorkflowAssociations = null;
            this.description = null;
            this.displayFormTemplateName = null;
            this.displayFormUrl = null;
            this.documentTemplate = null;
            this.documentTemplateUrl = null;
            this.editFormTemplateName = null;
            this.editFormUrl = null;
            this.group = null;
            this.hidden = null;
            this.id = null;
            this.jSLink = null;
            this.name = null;
            this.newFormTemplate = null;
            this.newFormUrl = null;
            this.readOnly = null;
            this.schemaXml = null;
            this.scope = null;
            this.sealed = null;
            this.urlFieldLinks = resultData.FieldLinks.__deferred.uri;
            this.urlFields = resultData.Fields.__deferred.uri;
            this.urlParent = resultData.Parent.__deferred.uri;
            this.urlWorkflowAssociations = resultData.WorkflowAssociations.__deferred.uri;
            this.description = resultData.Description;
            this.displayFormTemplateName = resultData.DisplayFormTemplateName;
            this.displayFormUrl = resultData.DisplayFormUrl;
            this.documentTemplate = resultData.DocumentTemplate;
            this.documentTemplateUrl = resultData.DocumentTemplateUrl;
            this.editFormTemplateName = resultData.EditFormTemplateName;
            this.editFormUrl = resultData.EditFormUrl;
            this.group = resultData.Group;
            this.hidden = resultData.Hidden;
            this.id = resultData.StringId;
            this.jSLink = resultData.JSLink;
            this.name = resultData.Name;
            this.newFormTemplate = resultData.NewFormTemplate;
            this.newFormUrl = resultData.NewFormUrl;
            this.readOnly = resultData.ReadOnly;
            this.schemaXml = resultData.SchemaXml;
            this.scope = resultData.Scope;
            this.sealed = resultData.Sealed;
        }
        return ContentType;
    })();
    SPREST.ContentType = ContentType;    
})(SPREST || (SPREST = {}));
